Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client | body.content undefined

Why am I getting this error message?

This is the only route via post. As far as I can see there is no other place where a header is sent.

I also don't understand why body.content evaluates to undefined,even though an object is sent and the header is set to json. By the way, the body parser or app.use(bodyParser.json()) didn't do anything.

"express": "^4.17.3"

I am thankful for help

const express = require("express");
const app = express();
app.use(express.json());

app.post("/api/persons/", (req, res) => {
  const body = req.body;
  //if body has no data return here
  console.log(body.content);
  if (!body.content) {
    return res.status(400).end().json({ error: "missing content" });
  }
//generate entry in list
  const person = {
    number: body.number,
    name: body.name,
    id: generateID(),
  };
  persons = persons.concat(person);
  res.json(person);
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

.end terminates the response. If you call .end, you can't send anything afterwards. Just remove that part and call .json to send the data to the client, without killing the response in the middle.

  if (!body.content) {
    return res.status(400).json({ error: "missing content" });
  }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda